package com.kob.backend.service.impl.gps;

import com.kob.backend.service.gps.GpsService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @program: backend
 * @description:
 * @author: mqf
 * @create: 2023-04-21 19:45
 **/
@Service
public class GpsServiceImpl implements GpsService {
    @Override
    public void sendFakeInfo() {
        String cmd = "D:/codingtool/PothosSDR/bin/hackrf_transfer -t D:/codingtool/PothosSDR/bin/gpssim.bin -f 1575420000 -s 2600000 -a 1 -x 0";
        doCommand(cmd);
    }
    void doCommand(String command) {
        try {
            // 执行命令
            Process process = Runtime.getRuntime().exec(command);

            // 命令的输出流
//            BufferedReader reader = new BufferedReader(
//                    new InputStreamReader(process.getInputStream()));
//            String line;

            final InputStream is1 = process.getInputStream();
            new Thread(() -> {
                BufferedReader br = new BufferedReader(new InputStreamReader(is1));
                try{
                    while(br.readLine() != null) ;
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
            }).start();
            InputStream is2 = process.getErrorStream();
            BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
            while(br2.readLine() != null){}





            // 输出命令执行结果
//            System.out.println("yyy");
//            while ((line = reader.readLine()) != null) {
//                System.out.println(line.getBytes().length);
//                System.out.println(line);
//            }
//            char[] buffer = new char[1024];
//            int length;
//            while ((length = reader.read(buffer)) != -1) {
//                System.out.print(new String(buffer, 0, length)); // 输出命令执行结果
//            }
//            System.out.println("xxxx");
            // 等待命令执行完成
            int i = process.waitFor();
            System.out.println(i);
//            if(!process.waitFor(1, TimeUnit.SECONDS)) {
//                //timeout - kill the process.
//                process.destroy(); // consider using destroyForcibly instead
//            }
        }catch (Exception e ) {
            e.printStackTrace();
        }
    }
}
